package com.project.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author lyl
 * mq
 */
@Configuration
public class MQConfig {
    @Autowired
    //注入工厂（已预加载）
    CachingConnectionFactory connectionFactory;
    //必须先注入connectionFactory，再注入rabbitAdmin
    @Autowired
    private RabbitAdmin rabbitAdmin;


    //rabbitMq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean(value = "rabbitAdmin")
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory);
    }

    /**
     * 与用户的通道1，用于提示信息
     *
     * @return
     */
    @Bean
    public Queue RiderQueue() {
        return new Queue("RiderQueue");
    }

    /**
     * 与用户的通道2，用于提示信息
     *
     * @return
     */
    @Bean
    public Queue RiderFinishOrderQueue() {
        return new Queue("RiderFinishOrderQueue");
    }

    /**
     * 与后台的通道1,发送订单
     *
     * @return
     */
    @Bean
    public Queue RiderSendOrderToBack() {
        return new Queue("RiderSendOrderToBack");
    }

    /**
     * 与后台的通道2，发送所有骑手详情
     *
     * @return
     */
    @Bean
    public Queue RiderSendListToBack() {
        return new Queue("RiderSendListToBack");
    }

    /**
     * 与后台的通道3,发起提现请求
     *
     * @return
     */
    @Bean
    public Queue RiderSendGetMoneyToBack() {
        return new Queue("RiderSendGetMoneyToBack");
    }

    /**
     * 与后台的通道4, 发送修改的骑手信息
     *
     * @return
     */
    @Bean
    public Queue RiderUpdateRiderInfoToBack() {
        return new Queue("RiderUpdateRiderInfoToBack");
    }

    //创建交换机：默认交换机、主题交换机、扇形交换机、头交换机
    //默认交换机
    @Bean
    public DirectExchange directExchange() {
        //给此交换机设置名字
        return new DirectExchange("riderDirectExchange");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueOneExchange(Queue RiderQueue, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderQueue).to(directExchange).with("Rider.Queue");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueTwoExchange(Queue RiderFinishOrderQueue, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderFinishOrderQueue).to(directExchange).with("Rider.Queue2");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueThreeExchange(Queue RiderSendOrderToBack, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderSendOrderToBack).to(directExchange).with("Rider.Queue3");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueFourExchange(Queue RiderSendListToBack, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderSendListToBack).to(directExchange).with("Rider.Queue4");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueFiveExchange(Queue RiderSendGetMoneyToBack, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderSendGetMoneyToBack).to(directExchange).with("Rider.Queue5");
    }

    @Bean
    //将队列绑定到默认交换机上
    public Binding queueSixExchange(Queue RiderUpdateRiderInfoToBack, DirectExchange directExchange) {
        //将queueOne队列绑定到directExchange交换机上         //给此绑定操作设置一个别名，因为mq原则是交换机加
        //连接名，所以用.的形式来取别名
        return BindingBuilder.bind(RiderUpdateRiderInfoToBack).to(directExchange).with("Rider.Queue6");
    }


    //进行交换机与信道添加，用于添加到远程mq
    @Bean
    public void createExchangeQueue() {
        rabbitAdmin.declareExchange(directExchange());
        //rabbitAdmin.declareExchange(directDeadExchange());
        rabbitAdmin.declareQueue(RiderQueue());
        rabbitAdmin.declareQueue(RiderFinishOrderQueue());


        rabbitAdmin.declareQueue(RiderSendOrderToBack());
        rabbitAdmin.declareQueue(RiderSendListToBack());
        rabbitAdmin.declareQueue(RiderSendGetMoneyToBack());
        rabbitAdmin.declareQueue(RiderUpdateRiderInfoToBack());
        //rabbitAdmin.declareQueue(queueDead());
    }
}
